package com.whfc.iot.fse.lift.util;

import com.whfc.common.util.JSONUtil;
import com.whfc.iot.fse.lift.codec.LiftMsgDataCodec;
import com.whfc.iot.fse.lift.codec.LiftMsgDataDecoderFactory;
import com.whfc.iot.fse.lift.entity.LiftConst;
import com.whfc.iot.fse.lift.entity.LiftMsg;
import com.whfc.iot.fse.lift.entity.LiftMsgFactory;
import com.whfc.iot.util.ByteUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;

/**
 * @Description: 升降机数据编解码工具
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-05-19 11:53
 */
public class LiftMsgUtil {

    /**
     * 解码
     *
     * @param buf
     * @return
     */
    public static LiftMsg decode(ByteBuf buf) {
        //跳过帧头
        buf.skipBytes(2);

        //数据类型
        int dataType = buf.readUnsignedByte();

        //吊笼
        int cage = buf.readUnsignedByte();
        int cageNo = cage >> 1;
        int cateType = cage & 0x01;

        //硬件SN
        byte[] deviceSnBytes = new byte[3];
        buf.readBytes(deviceSnBytes);
        int deviceSn = ByteUtil.low3ByteToInt(deviceSnBytes);

        //数据
        LiftMsg msg = LiftMsgFactory.createLiftMsg(dataType);
        LiftMsgDataCodec liftMsgDataCodec = LiftMsgDataDecoderFactory.getLiftMsgDataCodec(dataType);
        liftMsgDataCodec.decode(buf, msg);

        //校验值
        int checkSum = buf.readByte();

        //长度
        int length = buf.readUnsignedByte();

        //帧尾
        buf.skipBytes(4);

        msg.setDataType(dataType);
        msg.setCageNo(cageNo);
        msg.setCageType(cateType);
        msg.setDeviceSn(deviceSn);
        msg.setCheckSum(checkSum);
        msg.setLength(length);
        return msg;
    }

    /**
     * 编码
     *
     * @param msg
     * @return
     */
    public static void encode(ByteBuf buf, LiftMsg msg) {

        //帧头
        buf.writeShort(LiftConst.HEADER);

        //数据类型
        buf.writeByte(msg.getDataType());

        //吊笼
        int cage = (msg.getCageNo() << 1) | msg.getCageType();
        buf.writeByte(cage);

        //硬件SN
        byte[] deviceSnBytes = ByteUtil.extractLow3Byte(msg.getDeviceSn());
        buf.writeBytes(deviceSnBytes);

        //数据
        if (msg.getData() != null && msg.getData().length > 0) {
            buf.writeBytes(msg.getData());
        }

        //校验和
        buf.writeByte(msg.getCheckSum());

        //长度
        buf.writeByte(msg.getLength());

        //帧尾
        buf.writeInt(LiftConst.END_FLAG);
    }

    public static void main(String[] args) {

        ByteBuf buf = Unpooled.buffer();
        LiftMsg msg = new LiftMsg();
        msg.setDataType(0x01);
        msg.setCageNo(0x08);
        msg.setCageType(0x01);
        msg.setDeviceSn(0x000001);
        msg.setLength(0x07);
        msg.setCheckSum(0x12);

        LiftMsgUtil.encode(buf, msg);

        System.out.println(ByteBufUtil.prettyHexDump(buf));

        LiftMsg msg1 = LiftMsgUtil.decode(buf);

        System.out.println(JSONUtil.toString(msg));
        System.out.println(JSONUtil.toString(msg1));
    }
}
